package net.asfun.jvalog.resource;

import java.util.Collection;

import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;

import net.asfun.jvalog.common.Jdoer;
import net.asfun.jvalog.common.JdoerFactory;
import net.asfun.jvalog.entity.Message;
import net.asfun.jvalog.vo.Pager;

public class MessageDao {

	public static Collection<Message> listPublished(int size) {
		Jdoer<Message> jdm = JdoerFactory.jdoMessage;
		Query q = jdm.prepareQuery();
		q.setFilter("published == true");
		q.setOrdering("date descending");
		q.setRange(0, size);
		return jdm.more(q);
	}
	
	public static Collection<Message> listPublished(Key paper) {
		Jdoer<Message> jdm = JdoerFactory.jdoMessage;
		Query q = jdm.prepareQuery();
		q.setFilter("published == true && paper == mykey");
		q.declareParameters("com.google.appengine.api.datastore.Key mykey");
		return jdm.more(q, paper);
	}

	public static Collection<Message> listAll(Pager pager) {
		Jdoer<Message> jdm = JdoerFactory.jdoMessage;
		pager.setTotal(jdm.all().size());
		Query q = jdm.prepareQuery();
		q.setOrdering("published");
		q.setRange(pager.getStart(), pager.getEnd());
		return jdm.more(q);
	}
	
	public static Collection<Message> listAll(Key paper){
		Jdoer<Message> jdm = JdoerFactory.jdoMessage;
		Query q = jdm.prepareQuery();
		q.setFilter("paper == mykey");
		q.declareParameters("com.google.appengine.api.datastore.Key mykey");
		return jdm.more(q, paper);
	}
}
